package AddonsSheepContoller.ASPlayer;

import AddonsSheepContoller.Utils.EncryptionAlgorithm;
import AddonsSheepContoller.Utils.SQLiteManager;
import org.bukkit.plugin.Plugin;

import java.sql.ResultSet;
import java.sql.SQLException;

public class ASBankAccount {
    private final ASAccount playerASAccount;
    private final Plugin resourcePlugin;
    private final String databaseAddress;
    private String accountUid;
    private int accountChar; // "Char" is coin!
    private int accountIssues; // "Issues" is diamond!
    public boolean isEffective = false;

    public ASBankAccount(ASAccount playerAccount, Plugin resourcePlugin, String databaseAddress)
    { this.playerASAccount = playerAccount; this.resourcePlugin = resourcePlugin; this.databaseAddress = databaseAddress; }

    public void loginBankAccount(String uid, String password) throws SQLException {
        SQLiteManager loadingDatabase = new SQLiteManager(this.resourcePlugin);
        loadingDatabase.connectDatabase(this.databaseAddress);
        ResultSet resultSet = loadingDatabase.selectData("BankAccounts", new String[]{"uid", "password"});
        while(true) {
            if(!resultSet.next()) break;

            String getUid = resultSet.getString("uid");
            String getPassword = resultSet.getString("password");
            if(getUid.equalsIgnoreCase(uid))
                if(getPassword.equalsIgnoreCase(EncryptionAlgorithm.sha1(password))) {
                    this.accountUid = getUid;
                    this.isEffective = true;
                }
        }

        loadingDatabase.close();
    }

    public void updateAccountProperty() throws SQLException {
        if(!this.isEffective) return;

        SQLiteManager loadingDatabase = new SQLiteManager(this.resourcePlugin);
        loadingDatabase.connectDatabase(this.databaseAddress);
        ResultSet resultSet = loadingDatabase.selectData("BankAccounts", new String[]{"charQuantity", "issuesQuantity"});
        while(true) {
            if(!resultSet.next()) break;

            this.accountChar = resultSet.getInt("charQuantity");
            this.accountIssues = resultSet.getInt("issuesQuantity");
        }

        loadingDatabase.close();
    }

    public void writeAccountProperty()
    {
        if(!this.isEffective) return;

        SQLiteManager writeDatabase = new SQLiteManager(this.resourcePlugin);
        writeDatabase.connectDatabase(this.databaseAddress);
        writeDatabase.updateData("BankAccounts",
                "uid="+this.accountUid,
                "charQuantity="+this.accountChar+", issuesQuantity="+this.accountIssues);
        writeDatabase.close();
    }

    public ASAccount getPlayerAccount() { return this.playerASAccount; }

    public void setAccountChar(int cChar) { this.accountChar = cChar; }
    public void setAccountIssues(int cIssues) { this.accountIssues = cIssues; }

    public void addAccountChar(int cChar) { this.accountChar += cChar; }
    public void addAccountIssues(int cIssues) { this.accountIssues += cIssues; }
    public void delAccountChar(int cChar) { this.accountChar -= cChar; }
    public void delAccountIssues(int cIssues) { this.accountIssues -= cIssues; }

    public int getAccountCharQuantity() { return accountChar; }
    public int getAccountIssuesQuantity() { return accountIssues; }
    public String getUid() { return accountUid; }
}
